//
//  Enemy.m
//  BeatPlane
//
//  Created by LZXuan on 15-5-6.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "Enemy.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#define kScreenSize [UIScreen mainScreen].bounds.size
#define kEnemySpeed  2
@implementation Enemy

- (instancetype)init {
    if (self = [super init]) {
        self.image = [UIImage imageNamed:@"enemy2"];
        [self resetFrame];
        [self creatBombImages];
    }
    return self;
}

#pragma mark - 爆炸
- (void)creatBombImages {
    //创建动画数组
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < 6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"bomb%ld",i]];
        [arr addObject:image];
    }
    self.animationImages = arr;
    [arr release];
    
    self.animationDuration = 0.2;
    self.animationRepeatCount = 1;
}

- (void)bomb {
    //播放动画
    [self startAnimating];
    //爆炸声音 是短促的最好不要用AVAudioPlayer
    //AVAudioPlayer 播放音乐比较长
    //短促的声音(30s之内,比如短信声音比如消息声音)我们一般采用底层系统的播放声音
    //导入 头文件#import <AudioToolbox/AudioToolbox.h>
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"game_over" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    

    //1.声明一个系统声音id
    SystemSoundID soundID;
    //2.把这个声音id 和 app中的音频文件进行绑定
    //注册声音
    AudioServicesCreateSystemSoundID((CFURLRef) url, &soundID);
    //3.播放音频 操作soundID
    AudioServicesPlaySystemSound(soundID);
    
}




- (void)moveDown {
    CGRect frame = self.frame;
    if (frame.origin.y >= kScreenSize.height) {
        [self resetFrame];//重置
    }else {
        frame.origin.y += kEnemySpeed;
        self.frame = frame;
    }
}

- (void)resetFrame {
    //随机产生坐标
    NSInteger sub = kScreenSize.width-50;
    //-50---- -99.0
    CGFloat y = - (arc4random()%50+50.0);
    self.frame = CGRectMake(arc4random()%sub,y, 50, 50);
}



@end
